package de.tv.weatherox;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ForecastAttributeTransformer {
    public static MoonPhase transformMoonImageToMoonPhase(String moon) {
        //<img border=0 width=17 height=15 src="/pictures/moon/moon12.png">
        Pattern pattern = Pattern.compile("moon\\d*\\.png");
        Matcher matcher = pattern.matcher(moon);
        if (matcher.find()) {
            String moonphaseAsString = matcher.group();
            moonphaseAsString = StringUtils.chomp(moonphaseAsString, ".png");
            moonphaseAsString = StringUtils.strip(moonphaseAsString, "moon");
            int moonPhaseAsInt = NumberUtils.toInt(moonphaseAsString);
            if(moonPhaseAsInt == 1) {
                return MoonPhase.NEW;
            } else if(moonPhaseAsInt == 15) {
                return MoonPhase.FULL;
            } else if(moonPhaseAsInt > 15) {
                return MoonPhase.WANING;
            } else if(moonPhaseAsInt < 15) {
                return MoonPhase.WAXING;
            }
        }
        throw new IllegalArgumentException("No valid moonphase");
    }

    public static String transformToWindDirection(String wind) {
        //NNO 2
        return StringUtils.split(wind)[0];
    }

    public static int transformToWindForce(String wind) {
        //NNO 2
        String forceAsString = StringUtils.split(wind)[1];
        return NumberUtils.toInt(forceAsString);
    }

    public static int transformTemperatureStringToInt(String temperature) {
        //8°
        String temperatureAsString = StringUtils.chomp(temperature, "°");
        return NumberUtils.toInt(temperatureAsString);
    }

    public static int transformMillimeterStringToInt(String millimeter) {
        //7 mm
        String millimeterAsString = StringUtils.chomp(millimeter, " mm");
        return NumberUtils.toInt(millimeterAsString);
    }

    public static double transformCloudinessImageToValue(String cloudinessImage) {
        //<img border=0 width=17 height=17 src="/wetter/images/cloudx_4_8.png">
        String dividendAsString = StringUtils.right(cloudinessImage, 9);
        dividendAsString = StringUtils.left(dividendAsString, 1);
        double dividend = NumberUtils.toDouble(dividendAsString);
        double ret = dividend / 8d;
        return ret;
    }
}
